/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.huawei.app.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.msgsms.v2.MsgsmsClient;
import com.huaweicloud.sdk.core.region.Region;

import com.huaweicloud.sdk.msgsms.v2.model.CreateAppRequest;
import com.huaweicloud.sdk.msgsms.v2.model.CreateAppResponse;
import com.huaweicloud.sdk.msgsms.v2.model.ShowAppRequest;
import com.huaweicloud.sdk.msgsms.v2.model.ShowAppResponse;
import com.huaweicloud.sdk.msgsms.v2.model.SmsAppAddReq;
import com.huaweicloud.sdk.msgsms.v2.model.SmsAppUpdateReq;
import com.huaweicloud.sdk.msgsms.v2.model.UpdateAppRequest;
import com.huaweicloud.sdk.msgsms.v2.model.UpdateAppResponse;
import com.huaweicloud.sdk.msgsms.v2.region.MsgsmsRegion;

/**
 * 提供创建短信应用、获取应用详情、更新短信应用样例。
 */
public class MsgsmsAppManageDemo {
    public static void main(String[] args) {
        System.out.println("Start HUAWEI CLOUD MSGSMS App Manage Java Demo...");

        ICredential auth = getCredential("YOUR AK", "YOUR SK","YOUR PROJECT ID");

        // 创建MsgsmsClient实例
        MsgsmsClient client = getClient(MsgsmsRegion.CN_NORTH_4, auth);

        try {
            String appId = createApp(client);
            showApp(client, appId);
            updateApp(client, appId);
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    /**
     * 创建短信应用
     * @param client MsgsmsClient实例
     * @return 应用主键ID
     */
    private static String createApp(MsgsmsClient client) {
        CreateAppRequest request = new CreateAppRequest();
        SmsAppAddReq body = new SmsAppAddReq();
        body.setAppName("myTestApp");
        body.setRegion("cn");
        body.setEnterpriseProjectId("0");
        body.setEnterpriseProjectName("default");
        request.withBody(body);

        // 调用SDK的接口创建应用
        CreateAppResponse response = client.createApp(request);
        System.out.println(response.toString());
        return response.getId();
    }

    /**
     * 获取应用详情
     * @param client MsgsmsClient实例
     * @param appId 应用主键ID
     */
    private static void showApp(MsgsmsClient client, String appId) {
        ShowAppRequest request = new ShowAppRequest();
        request.setId(appId);

        // 调用SDK的接口获取应用详情
        ShowAppResponse response = client.showApp(request);
        System.out.println(response.toString());
    }

    /**
     * 更新短信应用
     * @param client MsgsmsClient实例
     * @param appId 应用主键ID
     */
    private static void updateApp(MsgsmsClient client, String appId) {
        UpdateAppRequest request = new UpdateAppRequest();
        SmsAppUpdateReq body = new SmsAppUpdateReq();
        body.setAppName("myTestAppNew");
        body.setRegion("cn");
        body.setEnterpriseProjectId("0");
        body.setEnterpriseProjectName("default");

        request.withBody(body);
        request.setId(appId);

        // 调用SDK的接口上传申请文件
        UpdateAppResponse response = client.updateApp(request);
        System.out.println(response.toString());
    }

    /**
     * 创建鉴权凭证
     * @param ak 租户账号对应的Access Key (AK)
     * @param sk 租户账号对应的Secret Access Key (SK)
     * @param projectId 租户账号对应的IAM项目ID
     * @return 鉴权凭证
     */
    public static ICredential getCredential(String ak, String sk, String projectId) {
        return new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);
    }

    /**
     * 创建Msgsms client
     * @param region region信息
     * @param auth 鉴权凭证
     * @return MsgsmsClient实例
     */
    public static MsgsmsClient getClient(Region region, ICredential auth) {
        // 使用默认配置
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        // 初始化Msgsms服务的客户端
        return MsgsmsClient.newBuilder()
                .withHttpConfig(config)
                .withCredential(auth)
                .withRegion(region)
                .build();
    }
}
